﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SubSonic;
using LinFen;

public partial class webUserControl_CustomerAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if(Request["ID"]!=null)
            {
                Query q = new Query("vw_CustomerInfo");
                q.AddWhere("ID", Request["ID"]);
                IDataReader dr = q.ExecuteReader();
                if(dr.Read())
                {
                    Panel2.Visible = false;
                    tbCustomerName.Enabled = false;
                    ddlLevel.Enabled = false;
                    ddlCityID.Enabled = false;
                    
                    AddItems(ddlCityID, "sysCityName", dr["CityID"].ToString(), 0);
                    
                    Query _q = new Query("sysDistrictName");
                    _q.AddWhere("cityid", ddlCityID.SelectedValue);
                    IDataReader _dr = _q.ExecuteReader();
                    while (_dr.Read())
                    {
                        ListItem li = new ListItem();
                        li.Value = _dr[0].ToString();
                        li.Text = _dr[1].ToString();
                        if(_dr[0].ToString()==dr["DistrictID"].ToString())
                        {
                            li.Selected = true;
                        }
                        ddlDistrictID.Items.Add(li);
                    }
                    tbCustomerName.Text = dr["CustomerName"].ToString();
                    tbAddress.Text = dr["addre"].ToString();
                    tbTelPhone.Text = dr["telphone"].ToString();
                    tbLinkMan.Text = dr["linkman"].ToString();
                    tbYeWu.Text = dr["yewu"].ToString();
                    tbPassWD1.Text = dr["adminpassWD"].ToString();
                    tbPassWD2.Text = tbPassWD1.Text;
                    RequiredFieldValidator2.Enabled = false;
                }
            }
            else
            {
                Cookies aCookies = new Cookies();
                int flag = int.Parse(aCookies.getCookies("cFlag"));
                int isType = int.Parse(aCookies.getCookies("cisType"));
                int districtID = int.Parse(aCookies.getCookies("cDistrictID"));
                int cityID = int.Parse(aCookies.getCookies("cCityID"));

                if (flag == 1)
                {
                    AddItems(ddlCityID, "sysCityName", cityID.ToString(), 0);
                    //ddlCityID.Enabled = false;

                    ddlDistrictID.Items.Clear();
                    Query q = new Query("sysDistrictName");
                    q.AddWhere("cityid", ddlCityID.SelectedValue);
                    IDataReader dr = q.ExecuteReader();
                    while (dr.Read())
                    {
                        ListItem li = new ListItem();
                        li.Value = dr[0].ToString();
                        li.Text = dr[1].ToString();
                        ddlDistrictID.Items.Add(li);
                    }
                }

                if (flag == 0)
                {
                    AddItems(ddlCityID, "sysCityName", cityID.ToString(), 0);
                    ddlCityID.Enabled = false;

                    ddlDistrictID.Items.Clear();
                    Query q = new Query("sysDistrictName");
                    q.AddWhere("cityid", ddlCityID.SelectedValue);
                    IDataReader dr = q.ExecuteReader();
                    while (dr.Read())
                    {
                        ListItem li = new ListItem();
                        li.Value = dr[0].ToString();
                        li.Text = dr[1].ToString();
                        if (districtID.ToString() == dr[0].ToString())
                        {
                            li.Selected = true;
                        }
                        ddlDistrictID.Items.Add(li);
                    }
                    //ddlDistrictID.Enabled = false;
                    ddlLevel.Enabled = false;
                } 
            }
           
        }
    }

    protected void btnSub_Click(object sender, EventArgs e)
    {
        if(Request["ID"]!=null)
        {
            Query q = new Query("Customerinfo");
            q.AddUpdateSetting("Addre", tbAddress.Text);
            q.AddUpdateSetting("telphone", tbTelPhone.Text);
            q.AddUpdateSetting("linkman", tbLinkMan.Text);
            q.AddUpdateSetting("yewu", tbYeWu.Text);
            
            q.AddWhere("id", Request["ID"]);
            q.Execute();
            
            Query _q=new Query("AdminInfo");
            if (tbPassWD1.Text != "")
            {
                 _q.AddUpdateSetting("adminpassWD", tbPassWD1.Text);
            }
            _q.AddUpdateSetting("districtID", ddlDistrictID.SelectedValue);
            _q.AddWhere("adminName", tbCustomerName.Text);
            _q.Execute();
            
            
            Response.Redirect("~/CustomerList.aspx");
        }
        else
        {
            Query q = new Query("Admininfo");
            q.AddWhere("AdminName", tbCustomerName.Text);
            if(q.GetRecordCount()==0)
            {
                CustomerInfo.Insert(tbCustomerName.Text, tbAddress.Text, tbTelPhone.Text, tbLinkMan.Text, tbYeWu.Text,
                                int.Parse(tbAllPoint.Text), 0, decimal.Parse(tbAllMoney.Text), 1, DateTime.Parse(tbOutDate.Text),DateTime.Parse(tbInDate.Text));
                AdminInfo.Insert(int.Parse(ddlCityID.SelectedValue), int.Parse(ddlDistrictID.SelectedValue), tbCustomerName.Text, tbPassWD1.Text, int.Parse(ddlLevel.SelectedValue), 0, 5);
                Literal1.Text = "<script language=javascript>alert('添加成功！')</script>";
                Response.Redirect("~/CustomerList.aspx");
            }
            else
            {
                Literal1.Text = "<script language=javascript>alert('已存在该客户，请重新添加！')</script>";
            }
        }
    }
    protected void ddlLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlDistrictID.Visible = ddlLevel.SelectedValue=="0";
        ddlCityID.Visible = ddlLevel.SelectedValue == "0";
    }
    static void AddItems(DropDownList ddl, string tb, string id, int isType)
    {
        if (isType == 1)
        {
            for (int i = 0; i <= 10; i++)
            {
                ListItem li = new ListItem();
                li.Value = i.ToString();
                li.Text = i.ToString();
                if (id == i.ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            }
        }
        else
        {
            Query q = new Query(tb);
            IDataReader dr = q.ExecuteReader();
            while (dr.Read())
            {
                ListItem li = new ListItem();
                li.Value = dr[0].ToString();
                li.Text = dr[1].ToString();
                if (id == dr[0].ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            }
        }

    }
    protected void ddlCityID_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlDistrictID.Items.Clear();
        Query q = new Query("sysDistrictName");
        q.AddWhere("cityid", ddlCityID.SelectedValue);
        IDataReader dr = q.ExecuteReader();
        while (dr.Read())
        {
            ListItem li = new ListItem();
            li.Value = dr[0].ToString();
            li.Text = dr[1].ToString();
            ddlDistrictID.Items.Add(li);
        }
    }
}
